package cc.yugu.yhdw.webserver.entity;

import cc.yugu.yhdw.common.constant.Constant;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.util.*;
/**
 * 用户信息实体封装
 *
 * @Date:Created in 2018/6/27 11:20
 * @Author:
 * @Modified By: zjn 2018/6/27 11:20
 */
public class SessionUser {

    private int id;
    private int agentId; //运营商
    private int agencyId; //代理商
    private int supplierId; //厂商
    private int rootAgentId;
    private double balance;
    private String username;
    private String realName;
    private String agentName;
    private String tel;
    private String memo;
    private String deptName;
    private String roleName;
    private String portrait;
    private List<File> tempFileList = new ArrayList<File>();
    private Set<String> operCodeSet = new HashSet<String>();
    private Map<Object, String> agentMap;
    private byte[] agentTree;
    private boolean admin;
    private int type;

    public String getTel() {
        return tel;
    }

    public void setTel(String tel) {
        this.tel = tel;
    }

    public String getMemo() {
        return memo;
    }

    public void setMemo(String memo) {
        this.memo = memo;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getAgentId() {
        return agentId;
    }

    public void setAgentId(int agentId) {
        this.agentId = agentId;
    }

    public int getAgencyId() {
        return agencyId;
    }

    public void setAgencyId(int agencyId) {
        this.agencyId = agencyId;
    }

    public int getSupplierId() {
        return supplierId;
    }

    public void setSupplierId(int supplierId) {
        this.supplierId = supplierId;
    }

    public int getRootAgentId() {
        return rootAgentId;
    }

    public void setRootAgentId(int rootAgentId) {
        this.rootAgentId = rootAgentId;
    }

    public double getBalance() {
        return balance;
    }

    public void setBalance(double balance) {
        this.balance = balance;
    }

    public List<File> getTempFileList() {
        return tempFileList;
    }

    public void setTempFileList(List<File> tempFileList) {
        this.tempFileList = tempFileList;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getRealName() {
        return realName;
    }

    public void setRealName(String realName) {
        this.realName = realName;
    }

    public String getAgentName() {
        return agentName;
    }

    public void setAgentName(String agentName) {
        this.agentName = agentName;
    }

    public String getDeptName() {
        return deptName;
    }

    public void setDeptName(String deptName) {
        this.deptName = deptName;
    }

    public String getRoleName() {
        return roleName;
    }

    public void setRoleName(String roleName) {
        this.roleName = roleName;
    }

    public String getPortrait() {
        return portrait;
    }

    public void setPortrait(String portrait) {
        this.portrait = portrait;
    }

    public byte[] getAgentTree() {
        return agentTree;
    }

    public void setAgentTree(byte[] agentTree) {
        this.agentTree = agentTree;
    }

    public boolean isAdmin() {
        return admin;
    }

    public void setAdmin(boolean admin) {
        this.admin = admin;
    }

    public void addTempFile(File f) {
        tempFileList.add(f);
    }

    public void deleteTempFile() {
        for (File f : tempFileList) {
            try {
                if (f.isFile()) {
                    f.delete();
                } else {
                    FileUtils.deleteDirectory(f);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public Set<String> getOperCodeSet() {
        return operCodeSet;
    }

    public void setOperCodeSet(Set<String> operCodeSet) {
        this.operCodeSet = operCodeSet;
    }

    public boolean hasOper(String operCode) {
        return id == Constant.ADMIN_USER_ID || operCodeSet.contains(operCode);
    }

    public boolean hasAnyOper(String[] operCodes) {
        if (id == Constant.ADMIN_USER_ID) {
            return true;
        }
        for (String code : operCodes) {
            if (operCodeSet.contains(code)) {
                return true;
            }
        }
        return false;
    }

    public void switchAgent(Integer agentId) {
        setAgentId(agentId);
        setAgencyId(0);
        setAgentName(agentMap.get(agentId));
    }

    public void switchAgency(Integer agentId, Integer agencyId) {
        setAgentId(agentId);
        setAgencyId(agencyId);
        setAgentName(agentMap.get(String.format("agency-%d-%d", agentId, agencyId)));
    }

    public Map<Object, String> getAgentMap() {
        return agentMap;
    }

    public void setAgentMap(Map<Object, String> agentMap) {
        this.agentMap = agentMap;
    }

    @Override
    public String toString() {
        return "SessionUser{" +
                "id=" + id +
                ", agentId=" + agentId +
                ", agencyId=" + agencyId +
                ", supplierId=" + supplierId +
                ", rootAgentId=" + rootAgentId +
                ", balance=" + balance +
                ", username='" + username + '\'' +
                ", realName='" + realName + '\'' +
                ", agentName='" + agentName + '\'' +
                ", tel='" + tel + '\'' +
                ", memo='" + memo + '\'' +
                ", deptName='" + deptName + '\'' +
                ", roleName='" + roleName + '\'' +
                ", portrait='" + portrait + '\'' +
                ", tempFileList=" + tempFileList +
                ", operCodeSet=" + operCodeSet +
                ", agentMap=" + agentMap +
                ", agentTree=" + Arrays.toString(agentTree) +
                ", admin=" + admin +
                ", type=" + type +
                '}';
    }
}
